#include "toast.h"
#include <QApplication>
#include <QScreen>

Toast::Toast(QWidget *parent)
    : QWidget(parent)
    , m_label(new QLabel(this))
    , m_timer(new QTimer(this))
{
    // 设置窗口属性
    setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
    setAttribute(Qt::WA_ShowWithoutActivating);
    setAttribute(Qt::WA_DeleteOnClose);

    // 设置布局
    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->setContentsMargins(15, 10, 15, 10);
    layout->addWidget(m_label);

    // 设置标签属性
    m_label->setAlignment(Qt::AlignCenter);
    m_label->setWordWrap(true);
    m_label->setMinimumWidth(200);
    m_label->setMaximumWidth(400);

    // 添加阴影效果
    auto shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setBlurRadius(20);
    shadowEffect->setColor(QColor(0, 0, 0, 80));
    shadowEffect->setOffset(0, 3);
    setGraphicsEffect(shadowEffect);

    // 连接定时器
    connect(m_timer, &QTimer::timeout, this, &Toast::onTimeout);
}

Toast::~Toast()
{
}

void Toast::showToast(const QString &message, QWidget *parent, ToastType type,
                      int timeoutMs, std::function<void()> onFinished)
{
    Toast *toast = new Toast(parent);
    toast->setupUI(message, type);

    // 如果有完成回调，连接信号
    if (onFinished) {
        connect(toast, &Toast::finished, toast, [onFinished, toast]() {
            onFinished();
            toast->deleteLater();
        });
    } else {
        connect(toast, &Toast::finished, toast, &Toast::deleteLater);
    }

    toast->startAnimation(timeoutMs);
}

void Toast::info(const QString &message, QWidget *parent, int timeoutMs)
{
    showToast(message, parent, Info, timeoutMs);
}

void Toast::success(const QString &message, QWidget *parent, int timeoutMs)
{
    showToast(message, parent, Success, timeoutMs);
}

void Toast::warning(const QString &message, QWidget *parent, int timeoutMs)
{
    showToast(message, parent, Warning, timeoutMs);
}

void Toast::error(const QString &message, QWidget *parent, int timeoutMs)
{
    showToast(message, parent, Error, timeoutMs);
}

void Toast::setupUI(const QString &message, ToastType type)
{
    m_label->setText(message);
    setToastStyle(type);

    // 调整大小
    adjustSize();
}

void Toast::startAnimation(int timeoutMs)
{
    // 显示窗口前先定位
    reposition();

    // 显示窗口
    show();
    raise(); // 确保窗口在最前面

    // 设置自动关闭定时器
    m_timer->start(timeoutMs);
}

void Toast::setToastStyle(ToastType type)
{
    QString styleSheet;

    switch (type) {
    case Info:
        styleSheet =
            "QLabel {"
            "color: #31708f;"
            "background-color: #d9edf7;"
            "border: 1px solid #bce8f1;"
            "border-radius: 8px;"
            "padding: 12px 16px;"
            "font-size: 14px;"
            "font-weight: 500;"
            "}";
        break;

    case Success:
        styleSheet =
            "QLabel {"
            "color: #3c763d;"
            "background-color: #dff0d8;"
            "border: 1px solid #d6e9c6;"
            "border-radius: 8px;"
            "padding: 12px 16px;"
            "font-size: 14px;"
            "font-weight: 500;"
            "}";
        break;

    case Warning:
        styleSheet =
            "QLabel {"
            "color: #8a6d3b;"
            "background-color: #fcf8e3;"
            "border: 1px solid #faebcc;"
            "border-radius: 8px;"
            "padding: 12px 16px;"
            "font-size: 14px;"
            "font-weight: 500;"
            "}";
        break;

    case Error:
        styleSheet =
            "QLabel {"
            "color: #a94442;"
            "background-color: #f2dede;"
            "border: 1px solid #ebccd1;"
            "border-radius: 8px;"
            "padding: 12px 16px;"
            "font-size: 14px;"
            "font-weight: 500;"
            "}";
        break;
    }

    m_label->setStyleSheet(styleSheet);
}

void Toast::reposition()
{
    QWidget *parent = parentWidget();
    if (parent) {
        // 获取父窗口的全局坐标和尺寸
        QRect parentRect = parent->frameGeometry();
        QPoint parentTopLeft = parent->mapToGlobal(QPoint(0, 0));

        // 计算 Toast 位置：水平居中，垂直方向在窗口的 1/3 处
        int x = parentTopLeft.x() + (parentRect.width() - width()) / 2;
        int y = parentTopLeft.y() + parentRect.height() / 3 - height() / 2;

        move(x, y);
    } else {
        // 如果没有父窗口，在屏幕的上1/3部分显示
        QScreen *screen = QApplication::primaryScreen();
        QRect screenGeometry = screen->availableGeometry();

        int x = screenGeometry.center().x() - width() / 2;
        int y = screenGeometry.top() + screenGeometry.height() / 3 - height() / 2;

        move(x, y);
    }
}

void Toast::onTimeout()
{
    emit finished(); // 发出完成信号
}
